﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Core;
using Portal.Systems.Model;
using Portal.Web.Utility;
using Portal.Web.Model;

namespace Portal.Systems.Render
{
    public class RoleRenderBll
    {
        static void CheckPermission(string SiteId)
        {
            try
            {
                string UserId = WebSessionUtility.GetCurrentLoginUser().UserId;
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, SystemPermission.StaticId, SystemPermission.funcManage, SystemPermission.funcManage, UserId, true);
            }
            catch (Exception ex)
            {
                string ReturnData = "<div class=CssAccessDenied>" + ex.Message.ToString() + " <span class=CssBack><a href=\"javascript:history.back(-1);\">Quay lại</a></span></div>\r\n";
                throw new Exception(ReturnData);
            }
        }

        static string GetJavascriptText(string SiteId)
        {
            string JavascripText =
                "<script language=javascript>\r\n" +
                   "    function ClientReading()\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        UserType = document.getElementById('txtSelectedUserType').value;\r\n" +
                   "        Keyword = document.getElementById('txtKeyword').value;\r\n" +
                   "        AjaxOut = Portal.Systems.Render.RoleRenderBll.ServerSideReading(SiteId, UserType, Keyword).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divListRoles').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "    }\r\n" +


                   "    function ClientAdd()\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        document.getElementById('divActionForm').style.display='block';\r\n" +
                   "        document.getElementById('divListForm').style.display='none';\r\n" +
                   "        AjaxOut = Portal.Systems.Render.RoleRenderBll.DrawAddForm(SiteId).value;\r\n" +
                   "        document.getElementById('divActionForm').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "        document.getElementById('txtRoleCode').focus();\r\n" +
                   "    }\r\n" +

                   "    function ClientUpdate(RolesId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        document.getElementById('divActionForm').style.display='block';\r\n" +
                   "        document.getElementById('divListForm').style.display='none';\r\n" +
                   "        AjaxOut = Portal.Systems.Render.RoleRenderBll.DrawUpdateForm(SiteId, RolesId).value;\r\n" +
                   "        document.getElementById('divActionForm').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "        document.getElementById('txtRoleCode').focus();\r\n" +
                   "    }\r\n" +

               

                   "    function ClientBack()\r\n" +
                   "    {\r\n" +
                   "        document.getElementById('divActionForm').style.display='none';\r\n" +
                   "        document.getElementById('divListForm').style.display='block';\r\n" +
                   "    }\r\n" +

                   "    function ClientAddRole()\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        RoleCode = document.getElementById('txtRoleCode').value;\r\n" +
                   "        RoleName = document.getElementById('txtRoleName').value;\r\n" +
                   "        ItemUserType=document.getElementById('txtSelectedUserType').value;\r\n" +
                   
                   "        Keyword= '';\r\n" +
                   "        if(document.getElementById('txtKeyword')!=null)\r\n" +
                   "        {\r\n" +
                   "            Keyword=document.getElementById('txtKeyword').value;\r\n" +
                   "        }\r\n" +
                   "        AjaxOut = Portal.Systems.Render.RoleRenderBll.ServerSideAdd(SiteId, RoleCode, RoleName, ItemUserType, Keyword).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divActionForm').innerHTML = '';\r\n" +
                   "        document.getElementById('divActionForm').style.display = 'none';\r\n" +
                   "        document.getElementById('divListRoles').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "        ClientBack();\r\n" +
                   "    }\r\n" +


                   "    function ClientDelete(RolesId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        if(confirm('Bạn đã chắc chắn xóa chưa?')==false)return;\r\n" +
                   "        ItemUserType=document.getElementById('txtSelectedUserType').value;\r\n" +
                   "        Keyword = document.getElementById('txtKeyword').value;\r\n" +
                   "        AjaxOut = Portal.Systems.Render.RoleRenderBll.ServerSideDelete(SiteId, RolesId, ItemUserType, Keyword).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divListRoles').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "    }\r\n" +

                   "    function ClientSaveRoles(RolesId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        LoginName = document.getElementById('txtRoleCode').value;\r\n" +
                   "        FullName = document.getElementById('txtRoleName').value;\r\n" +
                   "        UserType = document.getElementById('txtSelectedUserType').value;\r\n" +
                   "        Keyword = document.getElementById('txtKeyword').value;\r\n" +
                   "        AjaxOut = Portal.Systems.Render.RoleRenderBll.ServerSideSave(SiteId,RolesId, LoginName, FullName, UserType, Keyword).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divActionForm').innerHTML = '';\r\n" +
                   "        document.getElementById('divActionForm').style.display = 'none';\r\n" +
                   "        document.getElementById('divListRoles').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "        ClientBack();\r\n" +
                   "    }\r\n" +

                   //"    function ClientLoadRecord(Id)\r\n"+
                   //"    {\r\n"+
                   //"        document.getElementById('txtSelectedUserType').value=Id;\r\n"+
                   //"        ClientReading();\r\n"+
                   //"    }\r\n"+

                   "    function ClientAddAccountType()\r\n"+
                   "    {\r\n"+
                   //"        pat=document.getElementById('txtSelectedUserType').value;\r\n" +
                   "        pat='user';\r\n" +
                   "        window.open('AddAccountType.aspx?pat='+pat,'_self');\r\n" +
                   "    }\r\n"+

                   "    function ClientUpdateAccountType(AccountType)\r\n" +
                   "    {\r\n" +
                   "        pat=document.getElementById('txtSelectedUserType').value;\r\n" +
                   "        window.open('UpdateAccountType.aspx?AccountType='+pat,'_self');\r\n" +
                   "    }\r\n" +

                   "    function ClientDeleteAccountType()\r\n" +
                   "    {\r\n" +
                   "        AccountType=document.getElementById('txtSelectedUserType').value;\r\n" +
                   "        if(AccountType=='')\r\n" +
                   "        {\r\n"+
                   "            alert('Chưa xác định nhóm cần xóa!');\r\n"+
                   "            return;\r\n"+
                   "        }\r\n"+
                   "        if(confirm('Bạn đã chắc chắn xóa nhóm phân loại này chưa?')==false)return;\r\n"+
                   "        AjaxOut = Portal.Systems.Render.RoleRenderBll.ServerSideDeleteAccountType(SiteId, AccountType).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "         SelectedNode = $(\"#tree\").dynatree(\"getActiveNode\");\r\n" +
                   "         SelectedNode.remove();\r\n" +
                   "        document.getElementById('divListRoles').innerHTML=AjaxOut.HtmlContent;\r\n" +
                   "    }\r\n" +
                   "</script>\r\n";
            return JavascripText;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                CheckPermission(SiteId);
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string ButtonIconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
            
                string UserType = (string)WebEnvironments.Request(SiteId, "type");
                if (string.IsNullOrEmpty(UserType)) UserType = "";

                //AccountTypeCls[] AccountTypes = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingAccountType(SiteId, null);
                //string SelectAccountType =
                //    "<select id=drpSelectAccountTypeForList onchange=javascript:ClientReading();>\r\n" +
                //    "   <option value=\"\">Toàn bộ nhóm vị trí công việc</option>\r\n";
                //for (int iIndex = 0; iIndex < AccountTypes.Length; iIndex++)
                //{
                //    SelectAccountType += "    <option value=\"" + AccountTypes[iIndex].AccountType + "\">" + AccountTypes[iIndex].Description + "</option>\r\n";
                //}
                //SelectAccountType+=
                //    "</select>\r\n";
                //SelectAccountType += "</select>\r\n";

                string Html =
                   GetJavascriptText(SiteId)+
                   "<div id=divActionForm></div>\r\n" +
                   "<input type=hidden id=txtSelectedUserType value=\"" + UserType + "\">\r\n" +                   
                   "<div class=FormFunction id=divListForm>\r\n" +
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:ClientAddAccountType();\"><img class=image src=\"" + ButtonIconRoot + "/Add.png\"><div>Thêm mới nhóm</div></li>\r\n" +
                   //"            <li onclick=\"javascript:ClientUpdateAccountType();\"><img class=image src=\"" + ButtonIconRoot + "/Update.png\"><div>Sửa nhóm phân loại</div></li>\r\n" +
                   //"            <li onclick=\"javascript:ClientDeleteAccountType();\"><img class=image src=\"" + ButtonIconRoot + "/Delete.png\"><div>Xóa nhóm phân loại</div></li>\r\n" +

                   //"            <li onclick=\"javascript:ClientAdd();\"><img class=image src=\""+ButtonIconRoot+"/Add.png\"><div>Thêm mới vị trí</div></li>\r\n" +
                   "            <li onclick=\"javascript:window.open('../../folder.aspx?folder=admin','_self');\"><img class=image src=\"" + ButtonIconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +

                   "    <div class=CssListRole>\r\n" +
                   "        <table>\r\n" +
                   "        <tr>\r\n" +
                   //"            <td class=col1>\r\n" +
                   //"                <div class=CssTreeView>\r\n" +
                   //                     DrawTreeView(SiteId).HtmlContent +
                   //"                </div>\r\n" +
                   //"            </td>\r\n" +
                   "            <td class=col2>\r\n" +
                   "    <div class=CssFilter>Từ khóa lọc:<input type=textbox class=textbox id=txtKeyword><input type=button class=button value=\"Lọc theo từ khóa\" onclick=\"javascript:ClientReading();\"></div>\r\n" +
                   "                   <div id=\"divListRoles\" class=tabcontent>\r\n" +
                   ServerSideReading(SiteId, UserType, "").HtmlContent +
                   "                   </div>\r\n" +
                   "            </td>\r\n" +
                   "        </tr>\r\n" +
                   "        </table>\r\n" +
                   "    </div>\r\n" +
                   "</div>\r\n";
                   
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = "<div class=CssAccessDenied>"+ ex.Message.ToString()+"</div>\r\n";
            }
            
            return OAjaxOut;
        }



        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawTreeView(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                AccountTypeCls[] AccountTypes = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingAccountType(SiteId, null);
                string Html =
                              "<script language=javascript>\r\n" +
                              "    function ClientLoadRecord(Id)\r\n" +
                               "    {\r\n" +
                               "        document.getElementById('txtSelectedUserType').value=Id;\r\n" +
                               "        ClientReading();\r\n" +
                               "    }\r\n" +  
                              "</script>\r\n" +
                              

                              "<div class=TreeHolder>\r\n" +
                              "<div id=\"tree\">\r\n" +
                              "<ul>\r\n" +
                              " <li class=\"folder expanded\"><a href=\"root\">Danh sách nhóm</a>\r\n" +
                              "     <ul>\r\n";
                for (int iIndex = 0; iIndex < AccountTypes.Length; iIndex++)
                {
                    Html +=
                            "<li class=\"folder expanded\"><a href=\"" + AccountTypes[iIndex].AccountType + "\" target=\"_self\">" + AccountTypes[iIndex].Description + " (" + AccountTypes[iIndex].TotalRole.ToString("#,##0") + ")</a>\r\n" +
                            //"<li class=\"folder expanded\"><a href='#' onclick='javascript:ClientLoadRecord('" + AccountTypes[iIndex].AccountType + "')' target=\"_self\">" + AccountTypes[iIndex].Description + " (" + AccountTypes[iIndex].TotalRole.ToString("#,##0") + ")</a>\r\n" +
                                BuildSubTreeView(SiteId, AccountTypes[iIndex].AccountType).HtmlContent +
                            "</li>\r\n";
                }

                Html += "</ul>\r\n" +
                        "</li>\r\n" +
                        "</ul>\r\n" +
                        "</div>\r\n" +
                        "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }



        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut BuildSubTreeView(string SiteId, string ParentAccountType)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string Html = "";
                AccountTypeCls[] AccountTypes = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingAccountType(SiteId, ParentAccountType);
                Html +=
                    "<ul>\r\n";
                for (int iIndex = 0; iIndex < AccountTypes.Length; iIndex++)
                {
                    Html +=
                            "<li class=\"folder expanded\"><a target=\"_self\" href=\"" + AccountTypes[iIndex].AccountType + "\">" + AccountTypes[iIndex].Description + " (" + AccountTypes[iIndex].TotalRole.ToString("#,##0") + ")</a>\r\n" +                             
                                BuildSubTreeView(SiteId, AccountTypes[iIndex].AccountType).HtmlContent +
                            "</li>\r\n";
                }

                Html += "</ul>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideReading(string SiteId, string UserType, string Keyword)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                if (!string.IsNullOrEmpty(UserType))
                {
                    if (UserType.Equals("root")) UserType = "";
                }
                RoleCls[] Roles = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingRoles(SiteId, UserType, Keyword);
                if (Roles.Length == 0)
                {
                    OAjaxOut.HtmlContent = "<div class=CssNoData>Chưa có nhóm nào trong hệ thống</div>\r\n";
                    return OAjaxOut;
                }
                string Subject = "Danh sách nhóm truy cập";
                if (!string.IsNullOrEmpty(UserType))
                {
                    AccountTypeCls AccountType = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CreateAccountTypeModel(SiteId, UserType);
                    Subject = "Danh sách - " + AccountType.Description;
                }
                string Html =
                   "        <h3>Danh sách nhóm truy cập</h3>\r\n" +
                   "            <table>\r\n" +
                   "                <tr>\r\n" +
                   "                    <th class=thIndex>#</th>\r\n" +
                   "                    <th class=thCode>Mã</th>\r\n" +
                   "                    <th>Nhóm truy cập</th>\r\n" +
                   "                    <th></th>\r\n" +
                   "                    <th></th>\r\n" +
                   "                    <th></th>\r\n" +
                   "                    <th></th>\r\n" +
                   "                    <th></th>\r\n" +
                   "                </tr>\r\n";
                for (int iIndex = 0; iIndex < Roles.Length; iIndex++)
                {
                    string UpdateButton = "<input type=button class=button value=\"Sửa\" onclick=\"javascript:ClientUpdate('" + Roles[iIndex].RoleId + "');\">";
                    string DeleteButton = "<input type=button class=button value=\"Xóa\" onclick=\"javascript:ClientDelete('" + Roles[iIndex].RoleId + "');\">";
                    Html +=
                       "    <tr>\r\n" +
                       "        <td class=tdIndex>" + (iIndex + 1) + "</td>\r\n" +
                       "        <td class=tdCode>" + Roles[iIndex].RoleCode + "</td>\r\n" +
                       "        <td>" + Roles[iIndex].RoleName + "</td>\r\n" +
                       "        <td style=\"width:10px;\">"+UpdateButton+"</td>\r\n" +
                       "        <td style=\"width:10px;\">"+DeleteButton+"</td>\r\n" +
                       "        <td style=\"width:10px;\"><input type=button class=button value=\"Tài khoản\" onclick=\"window.open('UserRole.aspx?RoleId="+Roles[iIndex].RoleId+"','_self');\"></td>\r\n" +
                       "        <td style=\"width:10px;\"><input type=button class=button value=\"Menu\" onclick=\"window.open('RoleMenuManager.aspx?RoleId=" + Roles[iIndex].RoleId + "','_self');\"></td>\r\n" +
                       "        <td style=\"width:10px;\"><input type=button class=button value=\"Chuyển\" onclick=\"window.open('MoveRole.aspx?RoleId=" + Roles[iIndex].RoleId + "','_self');\"></td>\r\n" +
                       "   </tr>\r\n";
                }
                Html +=
                   "    </table>\r\n";
                   
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawAddForm(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string ButtonIconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
               
                string Html =
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:ClientAddRole();\"><img class=image src=\"" + ButtonIconRoot + "/Save.png\"><div>Ghi lại</div></li>\r\n" +
                   "            <li onclick=\"javascript:ClientBack();\"><img class=image src=\"" + ButtonIconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "<div class=CssFormSaveUpdate>\r\n"+
                   "  <h3>Thêm mới nhóm đối tượng</h3>\r\n" +
                   "  <table>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1>Mã nhóm:</td>\r\n" +
                   "        <td class=Col2><input id=txtRoleCode value=\"\" class=textbox type=textbox></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1>Đối tượng:</td>\r\n" +
                   "        <td class=Col2><input id=txtRoleName class=textbox type=textbox></td>\r\n" +
                   "    </tr>\r\n"+
                  
                   "  </table>\r\n" +
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawUpdateForm(string SiteId, string RoleId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string ButtonIconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
            
                RoleCls ORoles = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CreateRoleModel(SiteId, RoleId);
                string Html =
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:ClientSaveRoles('"+RoleId+"');\"><img class=image src=\"" + ButtonIconRoot + "/Save.png\"><div>Ghi lại</div></li>\r\n" +
                   "            <li onclick=\"javascript:ClientBack();\"><img class=image src=\"" + ButtonIconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "  <div class=CssFormSaveUpdate>\r\n"+
                   "  <h3>Sửa nhóm truy cập</h3>\r\n" +
                   "  <table>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1>Mã nhóm:</td>\r\n" +
                   "        <td class=Col2><input id=txtRoleCode class=textbox type=textbox value=\"" + ORoles.RoleCode + "\"></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1>Tên nhóm:</td>\r\n" +
                   "        <td class=Col2><input id=txtRoleName class=textbox type=textbox value=\"" + ORoles.RoleName + "\"></td>\r\n" +
                   "    </tr>\r\n" +
                   //"    <tr>\r\n" +
                   //"        <td class=c1></td>\r\n" +
                   //"        <td class=c2><input type=button class=button value=\"Chấp nhận\" onclick=\"javascript:ClientSaveRoles('"+RolesId+"');\"><input type=button class=button value=\"Quay lại\" onclick=\"javascript:ClientBack();\"></td>\r\n" +
                   //"    </tr>\r\n" +
                   "  </table>\r\n" +
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideAdd(
            string SiteId, 
            string RoleCode, 
            string RoleName, 
            string UserType,
            string Keyword)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                if (string.IsNullOrEmpty(RoleCode)) throw new Exception("Chưa nhập mã nhóm!");
                if (string.IsNullOrEmpty(RoleName)) throw new Exception("Chưa nhập tên nhóm!");
                string AssetCode = WebSessionUtility.GetCurrentLoginUser().AssetCode;
                if (UserType.Equals("root")) UserType = null;
                if (string.IsNullOrEmpty(UserType)) UserType = null;
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddRole(null, SiteId, AssetCode,null, RoleCode, RoleName, UserType);
                OAjaxOut.HtmlContent = ServerSideReading(SiteId, UserType, Keyword).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideSave(string SiteId, string RolesId, string LoginName,string FullName, string UserType, string Keyword)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                if (string.IsNullOrEmpty(LoginName)) throw new Exception("Chưa nhập mã truy cập!");
                if (string.IsNullOrEmpty(FullName)) throw new Exception("Chưa nhập tên truy cập!");
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).SaveRole(null, SiteId, RolesId, LoginName, FullName);
                OAjaxOut.HtmlContent = ServerSideReading(SiteId, UserType, Keyword).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideDelete(string SiteId, string RolesId, string UserType, string Keyword)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                if (UserType.Equals("root")) UserType = "";
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).DeleteRole(null, SiteId, RolesId);
                OAjaxOut.HtmlContent = ServerSideReading(SiteId, UserType, Keyword).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideDeleteAccountType(string SiteId, string AccountType)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).DeleteAccountType(SiteId, AccountType);
                OAjaxOut.HtmlContent = "<div class=CssNoData>Nhóm đã được xóa thành công</div>\r\n";
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }

    }
}
